package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.widget.SearchView;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseFragment;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.entity.BaseParams;
import com.isunland.managesystem.entity.ZTreeNode;
import com.isunland.managesystem.utils.MyStringUtil;
import com.isunland.managesystem.utils.MyUtils;
import com.isunland.managesystem.utils.SharedPreferencesUtil;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: wangyanan
 * 创建时间: 2017/6/3 18:08
 */
public class SelectLaunchListFragment extends BaseListFragment {

  private static final int MENU_ITEM_CLEAR = 1;
  private ArrayAdapter<String> mAdapter;
  private ArrayList<String> mList;
  private ArrayList<String> mListOriginal;

  @Override public String getUrl() {
    return null;
  }

  @Override public void resolveResponse(String json) {

  }

  @Override public void initData() {
    super.initData();
    setIsForbiddenVolley(true);//禁用volley

    mList = new ArrayList<>();
    mListOriginal = MyUtils.getActivitiesName(mActivity);
    Collections.sort(mListOriginal, new Comparator<String>() {
      public int compare(String mapping1, String mapping2) {
        return mapping1.compareTo(mapping2);
      }
    });
    mList.addAll(mListOriginal);
    mAdapter = new ArrayAdapter<>(mActivity, android.R.layout.select_dialog_singlechoice, mList);
  }

  @Override public void initView(View view, Bundle savedInstanceState) {
    super.initView(view, savedInstanceState);
    setTitleCustom("选择启动Activity");
    setListAdapter(mAdapter);
    //禁用下拉刷新
    getPullRefreshListView().setMode(PullToRefreshBase.Mode.DISABLED);
    showViewByType(SHOW_EMPTY_VIEW);//否则会显示loading.
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

    inflater.inflate(R.menu.menu_search, menu);
    menu.add(Menu.NONE, MENU_ITEM_CLEAR, 0, getString(R.string.clearChoose))
        .setIcon(R.drawable.delete)
        .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
    MenuItem menuItem = menu.findItem(R.id.menu_item_search);//在菜单中找到对应控件的item
    final SearchView searchView = (SearchView) MenuItemCompat.getActionView(menuItem);
    if (searchView == null) return;
    menuItem.expandActionView();//自动打开搜索框,弹出软键盘.
    //noinspection deprecation
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
      @Override public boolean onQueryTextSubmit(String query) {
        //ToastUtil.show("提交:"+query);
        return false;
      }

      @Override public boolean onQueryTextChange(String newText) {
        ArrayList<String> result = search(newText);
        mList.clear();
        mList.addAll(result);
        mAdapter.notifyDataSetChanged();
        return false;
      }
    });

    super.onCreateOptionsMenu(menu, inflater);
  }

  private ArrayList<String> search(String newText) {
    ArrayList<String> result = new ArrayList<>();
    if (MyStringUtil.isEmpty(newText)) {
      result = mListOriginal;
    } else {
      for (String name : mListOriginal) {
        if (name.toLowerCase().contains(newText.toLowerCase()) || MyStringUtil.getAllUpperCase(name)
            .toLowerCase()
            .contains(newText.toLowerCase())) {
          result.add(name);
        }
      }
    }
    return result;
  }

  @Override public void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    if (position < mListview.getHeaderViewsCount()) return;
    String select = mAdapter.getItem(position - l.getHeaderViewsCount());

    select(select);
  }

  private void select(String select) {
    SharedPreferencesUtil.setString(mActivity, SharedPreferencesUtil.KEY_LAUNCH_ACTIVITY, select);
    Intent i = new Intent();
    BaseParams<ZTreeNode> params = new BaseParams<>();
    params.setTitle(select);
    i.putExtra(BaseFragment.EXTRA_PARAMS, params);
    mActivity.setResult(Activity.RESULT_OK, i);
    mActivity.finish();
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == MENU_ITEM_CLEAR) {
      select("");
    }
    return super.onOptionsItemSelected(item);
  }
}
